<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BAuthItem
 *
 * @author jianfeng
 */
class BAuthItem {
    
    const RoleType_Mixed = "mixed";
    const Scope_All = 0;

    //put your code here
    private $id = null;
    private $name = null;
    private $app = null;
    private $type = null;
    private $showName = null;
    private $appId = null;

    public function __construct($id = null) {
        if ($id != null)
            $this->load($id);
    }

    public function load($id = null) {
        if ($id != null) {
            $this->id = $id;
            $mRole = BMApplicationAuthItem::model()->findByPk($id);
            $this->name = $mRole->authname;
            $this->showName = $mRole->showname;
            $this->appId = $mRole->app_id;
            $this->type = BMAuthItemType::model()->findByPk($mRole->authtype)->typename;
        }
    }

    public function getId() {
        return $this->id;
    }

    public function getName() {
        return $this->name;
    }

    public function getShowName() {
        return $this->showName;
    }

    public function getAuthType() {
        return $this->type;
    }

    public function getApp() {
        if ($this->app == NULL)
            $this->app = BApp::getInstanceById($this->appId);
        return $this->app;
    }

    public function isAdminType() {
        $type = $this->getAuthType();

        if ($type == BBasicRolesTemplate::SERVEDTEAM_ADMIN_ROLE ||
                $type == BBasicRolesTemplate::Space_ADMIN_ROLE ||
                $type == BBasicRolesTemplate::WORKTEAM_ADMIN_ROLE)
            return TRUE;

        return FALSE;
    }

    public function isMemberType() {
        $type = $this->getAuthType();

        if ($type == BBasicRolesTemplate::SERVEDTEAM_MEMBER_ROLE ||
                $type == BBasicRolesTemplate::Space_MEMBER_ROLE ||
                $type == BBasicRolesTemplate::WORKTEAM_MEMBER_ROLE)
            return TRUE;

        return FALSE;
    }
    
    /**
     * use this function to find permitObject under a App
     * @param type $data
     * @return array array of permitObjects
     */
    public static function findAuthItems($appId = NULL, $spaceId = NULL, $data = array()) {

        $appId = $appId == NULL ? Blyn::app()->getActiveApp()->getId() : $appId;
        $spaceId = $spaceId == NULL ? Blyn::app()->getActiveApp()->getId() : $spaceId;

        $attributes = array();

        $attributes['app_id'] = $appId;
        $roleName = isset($data['roleName']) ? $data['roleName'] : NULL;
        if ($roleName != NULL)
            $attributes['authname'] = $roleName;

        $mRoles = BMApplicationAuthItem::model()->findAllByAttributes($attributes);
        $roles = array();

        /**
         * if space_id == null, means it used for all Space
         */
        foreach ($mRoles as $mRole) {
            if ($mRole->space_id == NULL || $mRole->space_id == $spaceId) {
                $role = new BAuthItem($mRole->_id);
                $roles[$mRole->_id] = $role;
            }
        }

        return $roles;
    }


}

?>
